import React, { useState, useEffect, useMemo } from "react";
import { connect } from "react-redux";
import { Toast } from "antd-mobile";
import MainLayout from "@/component/mainLayout/mainLayout";
import yhTools from "@/tools/yh";
import { InterfaceG } from "@/config/Config";
import Form from "./components/form";
import OtherInfo from "./components/otherInfo";
import Button from "../components/button";
import creditStore from "../creditStore";
import "./index.scss";

const linkmanArr = [
	{
		value: "02",
		label: "父母"
	},
	{
		value: "01",
		label: "配偶"
	},
	{
		value: "00",
		label: "子女"
	},
	{
		value: "03",
		label: "亲属"
	},
	{
		value: "04",
		label: "朋友"
	},
	{
		value: "05",
		label: "同事"
	}
];

const Linkman = props => {
	const { productNum } = props;
	const [values, setValues] = useState({});
	const valuesArr = useMemo(() => [], []); //获取value的函数
	const linkManCount = yhTools.getUrlParam("linkManCount"); //获取地址linkManCount参数，1-不显示联系人2
	const xhdCreditApplyingQueryPersonalInfo = async () => {
		let objs = {
			url: InterfaceG.xhdCreditApplyingQueryPersonalInfo,
			params: {
				productNum
			}
		};
		try {
			Toast.loading("加载中...");
			const { code, data } = await yhTools.request(objs);
			if (code === "0" && data) {
				setValues(data);
			}
		} catch (error) {
			Toast.fail(error ? error.message : "");
		}
	};
	useEffect(() => {
		xhdCreditApplyingQueryPersonalInfo();
	}, []);
	// 查询

	const getValuesFn = fn => {
		valuesArr.push(fn);
	};
	//个人信息
	const submit = async () => {
		try {
			const dataArr = await Promise.all(valuesArr.map(fn => fn()));
			const contactsInfoDtoList = [];
			const url =
				props.currentCode === "802"
					? InterfaceG.savePersonalInfoAndSubmitCredit
					: InterfaceG.xhdCreditSavePersonalInfo;
			let params = {};
			// 组装数据
			dataArr.forEach(item => {
				if (item.contactType) {
					if (item.userContactsVariableDto) {
						params.userContactsVariableDto = item.userContactsVariableDto;
					}
					delete item.userContactsVariableDto;
					contactsInfoDtoList.push(item);
				} else {
					params = { ...params, ...item };
				}
			});
			let objs = {
				url,
				params: {
					productNum,
					...params,
					contactsInfoDtoList
				}
			};
			try {
				Toast.loading("加载中...");
				const { code, message } = await yhTools.request(objs);
				if (code == "0") {
					props.goNext();
				} else {
					Toast.info(message, 2);
				}
			} catch (error) {
				Toast.fail(error ? error.message : "");
			}
		} catch (error) {
			console.log(error);
		}
	};
	const headerParams = {
		header: {
			title: "完善资料"
		},
		componentDidRecover: () => {
			xhdCreditApplyingQueryPersonalInfo();
		},
		isModal: true,
		history: props.history,
		cacheLifecycles: props.cacheLifecycles
	};
	return (
		<MainLayout pageParams={headerParams} handleBack={props.handleBack}>
			<div className="linkman-wrapper">
				<Form
					values={values}
					index="1"
					linkmanArr={linkmanArr.slice(0, 3)}
					getValuesFn={getValuesFn}
				/>
				{linkManCount == "1" || (
					<Form
						values={values}
						index="2"
						linkmanArr={linkmanArr.slice(3)}
						getValuesFn={getValuesFn}
					/>
				)}
				<OtherInfo values={values} getValuesFn={getValuesFn} />
				<Button isHiddenBotton={props.isHiddenBotton} onClick={submit}>
					确定
				</Button>
			</div>
		</MainLayout>
	);
};
const mapStateToProps = state => state;

export default connect(mapStateToProps)(creditStore(Linkman));
